package com.jesterplayer.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Audio.AlbumColumns;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.jesterplayer.R;
import com.jesterplayer.adapter.ArtistAlbumAdapter;

public class ArtistFragment extends SherlockFragment implements
		LoaderCallbacks<Cursor>, OnItemClickListener {
	 public final static String EXTERNAL = "external";
	// Adapter
	private ArtistAlbumAdapter mArtistAlbumAdapter;

	// Audio columns
	public static int mAlbumIdIndex, mAlbumNameIndex, mSongCountIndex,
			mArtistNameIndex;

	// ListView
	private ListView mListView;

	// Options
	private final int PLAY_SELECTION = 15;

	private final int ADD_TO_PLAYLIST = 16;

	private final int SEARCH = 17;

	// Album ID
	private String mCurrentAlbumId;

	// Cursor
	private Cursor mCursor;

	public ArtistFragment() {
	}

	public ArtistFragment(Bundle args) {
		setArguments(args);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// AlbumAdapter
		mArtistAlbumAdapter = new ArtistAlbumAdapter(getActivity(),
				R.layout.listview_items, null, new String[] {}, new int[] {}, 0);
		mListView.setOnCreateContextMenuListener(this);
		mListView.setAdapter(mArtistAlbumAdapter);
		mListView.setOnItemClickListener(this);

		// Important!
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.listview, container, false);
		mListView = (ListView) root.findViewById(android.R.id.list);

		return root;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		String[] projection = { BaseColumns._ID, AlbumColumns.ALBUM,
				AlbumColumns.NUMBER_OF_SONGS, AlbumColumns.ARTIST };
		if (getArguments() != null) {
			long artistId = getArguments().getLong((BaseColumns._ID));
			Uri uri = Audio.Artists.Albums.getContentUri(EXTERNAL, artistId);
			String sortOrder = Audio.Albums.DEFAULT_SORT_ORDER;
			return new CursorLoader(getActivity(), uri, projection, null, null,
					sortOrder);
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Check for database errors
		if (data == null) {
			return;
		}

		mAlbumIdIndex = data.getColumnIndexOrThrow(BaseColumns._ID);
		mAlbumNameIndex = data.getColumnIndexOrThrow(AlbumColumns.ALBUM);
		mSongCountIndex = data
				.getColumnIndexOrThrow(AlbumColumns.NUMBER_OF_SONGS);
		mArtistNameIndex = data.getColumnIndexOrThrow(AlbumColumns.ARTIST);
		mArtistAlbumAdapter.changeCursor(data);
		mCursor = data;
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		if (mArtistAlbumAdapter != null)
			mArtistAlbumAdapter.changeCursor(null);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putAll(getArguments() != null ? getArguments() : new Bundle());
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	}

	/**
	 * Update the list as needed
	 */
	private final BroadcastReceiver mMediaStatusReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (mListView != null) {
				mArtistAlbumAdapter.notifyDataSetChanged();
			}
		}

	};

	@Override
	public void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter();
		getActivity().registerReceiver(mMediaStatusReceiver, filter);
	}

	@Override
	public void onStop() {
		getActivity().unregisterReceiver(mMediaStatusReceiver);
		super.onStop();
	}


	/*
	 * public View onCreateView(LayoutInflater inflater, ViewGroup container,
	 * Bundle savedInstanceState) { View view =
	 * inflater.inflate(R.layout.activity_library, container, false); return
	 * view; }
	 * 
	 * public void setText(String item) { TextView view = (TextView)
	 * getView().findViewById(R.id.text_context); view.setText(item); }
	 */
}
